#include <jni.h>
#include <string>
#include "zlib.h"

#include <android/log.h>

#define  LOG_TAG  "compress"
#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGD(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)


extern "C" JNIEXPORT jstring JNICALL
Java_com_example_compress_NativeLib_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";


    return env->NewStringUTF(hello.c_str());
}


extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_compress_NativeLib_compressString(JNIEnv *env, jobject thiz, jstring info) {
    //存储压缩后的数据
    Bytef compresed[100] = {0};
    uLongf compressed_len;

    const Bytef data[] = "helloworld";
    uLong data_len = (uLong) sizeof(data);
    //存储解压后的数据
    Bytef uncompressed_data[100];
    uLong uncompressed_data_len;

    //compressed_len 获取压缩后的数据长度
    compress(compresed, &compressed_len, data, data_len);

    //uncompressed_data_len 获取解压缩后的数据的长度
    uncompress(uncompressed_data, &uncompressed_data_len, compresed, compressed_len);

    printf("%ld :%s\n", uncompressed_data_len, uncompressed_data);


    std::string hello = "Java_com_example_compress_NativeLib_compressString";
    LOGE("dongtai=%ld :%s\n", compressed_len, compresed);

    return env->NewStringUTF(hello.c_str());

}
extern "C"
JNIEXPORT jbyteArray JNICALL
Java_com_example_compress_NativeLib_compress(JNIEnv *env, jobject thiz, jbyteArray info) {

    //convert to jbyte array
    jbyte *init_bytes = (jbyte *) env->GetByteArrayElements(info, 0);

    //get jbyteArray size
    jint init_size = env->GetArrayLength(info);

    //force  convert to byte *
    Bytef *bytearr = (Bytef *) init_bytes;




    //Byte compr[200], uncompr[200]; //足够大
    uLong comprLen, uncomprLen;

    Byte *compr = new Byte;

    int err;
    err = compress(compr, &comprLen, bytearr, init_size);

    if (err != Z_OK) {
        LOGE("压缩失败");
    }
    LOGE("压缩后长度 :%lu", comprLen);
    jbyte *by = (jbyte *) compr;
    LOGE("nOutSize :%d", comprLen);
    jbyteArray jarray = env->NewByteArray(comprLen);
    env->SetByteArrayRegion(jarray, 0, comprLen, by);
    return jarray;

}








extern "C"
JNIEXPORT jbyteArray JNICALL
Java_com_example_compress_NativeLib_uncompress(JNIEnv *env, jobject thiz,
                                               jbyteArray info) {

    //convert to jbyte array
    jbyte *init_bytes = (jbyte *) env->GetByteArrayElements(info, 0);

    //get jbyteArray size
    jint init_size = env->GetArrayLength(info);

    //force  convert to byte *
    Bytef *bytearr = (Bytef *) init_bytes;




    //Byte compr[200], uncompr[200]; //足够大
    uLong comprLen, uncomprLen;

    Byte *compr = new Byte;

    int err;
    err = uncompress(compr, &comprLen, bytearr, init_size);

    if (err != Z_OK) {
        LOGE("解压失败");
    }
    LOGE("解压后长度 :%lu", comprLen);
    jbyte *by = (jbyte *) compr;
    LOGE("nOutSize :%d", comprLen);
    jbyteArray jarray = env->NewByteArray(comprLen);
    env->SetByteArrayRegion(jarray, 0, comprLen, by);
    return jarray;
}